﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TimeTracker_BL.Repositories;
using Telerik.Web.Mvc;
using TimeTracker_Entities;
using TimeTracker.Models;

namespace TimeTracker.Controllers
{
    [Authorize(Users="dbosssbzz@gmail.com")]
    public class ClientController : Controller
    {
        private ITimeTrackerRepository _db;
        private ILookupsRepository _lookups;

        /// <summary>
        /// Initializes a new instance of the <see cref="ClientController"/> class.
        /// </summary>
        public ClientController()
        {
            _db = new TimeTrackerRepository();
            _lookups = new LookupsRepository();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ClientController"/> class.
        /// </summary>
        /// <param name="db">The entity db.</param>
        /// <param name="lookups">The lookups.</param>
        public ClientController(ITimeTrackerRepository db, ILookupsRepository lookups)
        {
            _db = db;
            _lookups = lookups;
        }

        public ActionResult Index()
        {
            PopulateBillingTermsLookup();
            return View();
        }

        /// <summary>
        /// Gets the clients from the database.
        /// </summary>
        [GridAction]
        public ActionResult _GetClients()
        {
            // Get the list of clients from the database
            var clients = from c in _db.GetClients()
                          select new ClientModel(c);

            return View(new GridModel<ClientModel>(clients));
        }

        [GridAction]
        public ActionResult _AddClient(Client clientToAdd)
        {
            // Add the client to the database
            var result = _db.AddClient(clientToAdd);

            // Return either the updated list or an error message
            if (result.IsSuccessful)
            {
                return _GetClients();
            }
            else
            {
                return Content(result.ErrorMessage);
            }
        }

        [GridAction]
        public ActionResult _UpdateClient(Client updatedClient)
        {
            // Update the client in the database
            var result = _db.UpdateClient(updatedClient);

            // Return either the updated list or an error message
            if (result.IsSuccessful)
            {
                return _GetClients();
            }
            else
            {
                return Content(result.ErrorMessage);
            }
        }

        [GridAction]
        public ActionResult _DeleteClient(int deletedClientId)
        {
            // Delete the client from the database
            var result = _db.DeleteClient(deletedClientId);

            // Return either the updated list or an error message
            if (result.IsSuccessful)
            {
                return _GetClients();
            }
            else
            {
                return Content(result.ErrorMessage);
            }
        }

        /// <summary>
        /// Populates the billing terms lookup.
        /// </summary>
        private void PopulateBillingTermsLookup()
        {
            ViewBag.BillingTerms = _lookups.GetBillingTerms();
        }
    }
}
